package com.ruoyi.bizsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bizsys.domain.WalletOrder;
import com.ruoyi.bizsys.mapper.WalletOrderMapper;
import com.ruoyi.bizsys.service.IWalletOrderService;
import com.ruoyi.bizsys.vo.WalletOrderVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 钱包订单Service业务层处理
 *
 * @author african-loan
 * @date 2022-06-17
 */
@Service
public class WalletOrderServiceImpl extends ServiceImpl<WalletOrderMapper, WalletOrder> implements IWalletOrderService {

    /**
     * 查询钱包订单
     *
     * @param id 钱包订单ID
     * @return 钱包订单
     */
    @Override
    public WalletOrder selectWalletOrderById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询钱包订单列表
     *
     * @param walletOrder 钱包订单
     * @return 钱包订单
     */
    @Override
    public List<WalletOrder> selectWalletOrderList(WalletOrder walletOrder) {
        if(walletOrder.getParams().entrySet().size() > 0){
            return this.list(new QueryWrapper<>(walletOrder).ge("create_time",walletOrder.getParams().get("beginTime")).lt("create_time",walletOrder.getParams().get("endTime")).last("order by id desc"));
        }
        return this.list(new QueryWrapper<>(walletOrder).last("order by id desc"));
    }

    /**
     * 新增钱包订单
     *
     * @param walletOrder 钱包订单
     * @return 结果
     */
    @Override
    public int insertWalletOrder(WalletOrder walletOrder) {
        walletOrder.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(walletOrder);
    }

    /**
     * 修改钱包订单
     *
     * @param walletOrder 钱包订单
     * @return 结果
     */
    @Override
    public int updateWalletOrder(WalletOrder walletOrder) {
        walletOrder.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(walletOrder);
    }

    /**
     * 批量删除钱包订单
     *
     * @param ids 需要删除的钱包订单ID
     * @return 结果
     */
    @Override
    public int deleteWalletOrderByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除钱包订单信息
     *
     * @param id 钱包订单ID
     * @return 结果
     */
    @Override
    public int deleteWalletOrderById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<WalletOrderVo> selectWalletList(WalletOrder walletOrder) {
        List<WalletOrderVo> walletOrderVos = baseMapper.selectWalletList(walletOrder);
        List<WalletOrderVo> walletOrderList = new ArrayList<>();
        //DecimalFormat df2 = new DecimalFormat("#,###.00");//千分位格式化
        walletOrderVos.forEach(e -> {
            WalletOrderVo walletOrderVo = new WalletOrderVo();
            walletOrderVo.setBankName(e.getBankName());
            walletOrderVo.setWalletServiceFee(e.getServiceFee().setScale(2, BigDecimal.ROUND_UP));
            walletOrderVo.setWalletType(e.getWalletType());
            walletOrderVo.setCreateTime(e.getCreateTime());
            walletOrderVo.setWalletAmount(e.getAmount().setScale(2, BigDecimal.ROUND_UP));
            walletOrderVo.setOrderStatus(e.getOrderStatus());
            walletOrderVo.setUpdateTime(e.getUpdateTime());
            walletOrderVo.setBankAccount(e.getBankAccount());
            walletOrderVo.setAmount(e.getAmount());
            walletOrderVo.setOrderType(e.getOrderType());
            walletOrderList.add(walletOrderVo);
        });
        return walletOrderList;
    }


}
